var ttt= true;
$(function(){
	init();
}); 

function init(){
	initbtn();
	initgallery();	//gallery configure
}

initbtn = function(){
	$("#autobtn").click(function(){
		var key = $(this).attr("key");
		if(key=="on"){
			$(this).html("<i class='icon-circle-arrow-up i_href'></i>自动");
		}else{
			$(this).html("<i class='i_href icon-stop'></i>手动");
		}
		autofn(key);
		key = key == "on" ? "off" : "on";
		$(this).attr("key",key);
	});	
	
	$("#toggle-fullscreen").click(function(){
		var key = $(this).attr("key");
		if(key=="on"){
			$(this).html("<i class='icon-fullscreen i_href'></i>全屏");
		}else{
			$(this).html("<i class='icon-resize-small i_href '></i>标准");
		}
		key = key == "on" ? "off" : "on";
		$(this).attr("key",key);
	});	
	
	$("#vfn").click(function(){
		var key = $(this).attr("key");
		if(key=="wall"){
			$(this).html("<i class='icon-th i_href'></i>图片墙模式");
		}else{
			$(this).html("<i class='icon-facetime-video i_href '></i>幻灯片模式");
		}
		pw.view = key;//设置显示模式 
		key = key == "wall" ? "ppt" : "wall";
		$(this).attr("key",key);
	});	
	$(".hero-unit").mouseenter(function(){
		$(".btn-group").toggle();
	});
	$(".hero-unit").mouseleave(function(){
		$(".btn-group").toggle();
	});
	
	cc = function(){
		window.opener=null;
	    window.open('', '_self', ''); 
	    window.close(); 
	}
}

autofn = function(key){
	if(key=="on"){
		pw.overint = setInterval("getphoto()",pw.overtime);
	}else{
		clearInterval(pw.overint);
	}	
}

getphoto = function(){
	//动态获取后台活动图片,定时获取新图片
    //[1](暂时由后台完成获取图片动作)获取数据库中所有的图片
    //[2]启动定时回访后台数据库的接口
        
    // Load images via flickr for demonstration purposes:
	pw.url = site.web+"my/photowall/g";
	
	cb = function(data){
		if(data.code==1){
			$.each(data.data.photo, function (index, photo) {
	            $('<a data-gallery="gallery"/>')
	                .append($('<img>').prop('src', site.web + photo.fileimg))
	                .prop('href', site.web + "files/" + photo.filepath)
	                .prop('title', photo.filedesc)
	                .prependTo(gallery);
	        });
			pw.fid = data.data.fid;
			if(data.data.total==0){//有图片10秒更新一次,没有图片1分钟更新一次
				pw.overtime = "60000";
			}else{
				//当有更新时,重新刷新页面
				if(pw.view=="ppt"){
					$("#eid").val(pw.eid);
					$("#submitform").click();
				}
				pw.overtime = "10000";				
			}			
			pw.total = data.data.total + parseInt(pw.total);
			pw.requesttime ++;
		}
	}
	G(pw,cb);
	
   /* $.ajax({
        url: 'http://api.flickr.com/services/rest/',
        data: {
            format: 'json',
            method: 'flickr.interestingness.getList',
            api_key: '7617adae70159d09ba78cfec73c13be3'
        },
	    dataType: 'jsonp',
        jsonp: 'jsoncallback'
    }).done(function (data) {
        var gallery = $('#gallery'),
            url;
        $.each(data.photos.photo, function (index, photo) {
            url = 'http://farm' + photo.farm + '.static.flickr.com/' +
                photo.server + '/' + photo.id + '_' + photo.secret;
            $('<a data-gallery="gallery"/>')
                .append($('<img>').prop('src', url + '_s.jpg'))
                .prop('href', url + '_b.jpg')
                .prop('title', photo.title)
                .appendTo(gallery);
        });
    });	*/
}

initgallery = function(){
	    // Start slideshow button:
	    $('#start-slideshow').button().click(function () {
	        var options = $(this).data(),
	            modal = $(options.target),
	            data = modal.data('modal');
	        if (data) {
	            $.extend(data.options, options);
	        } else {
	            options = $.extend(modal.data(), options);
	        }
	        modal.find('.modal-slideshow').find('i')
	            .removeClass('icon-play')
	            .addClass('icon-pause');
	        modal.modal(options);
	    });

	    // Toggle fullscreen button:
	    $('#toggle-fullscreen').button().click(function () {
	        var button = $(this),
	            root = document.documentElement;
	        if (!button.hasClass('active')) {
	            $('#modal-gallery').addClass('modal-fullscreen');
	            if (root.webkitRequestFullScreen) {
	                root.webkitRequestFullScreen(
	                    window.Element.ALLOW_KEYBOARD_INPUT
	                );
	            } else if (root.mozRequestFullScreen) {
	                root.mozRequestFullScreen();
	            }
	        } else {
	            $('#modal-gallery').removeClass('modal-fullscreen');
	            (document.webkitCancelFullScreen ||
	                document.mozCancelFullScreen ||
	                $.noop).apply(document);
	        }
	    });
	    //TODO: REFLASH THE SELECTOR CONTAINER BY VINCENT 20130817
	    $('#modal-gallery').on('show', function () {
	        var modalData = $(this).data('modal');
	        //alertjson(modalData.options);
	        //if(ttt)
	        //modalData.$links.push("http://localhost/mm/files20130808/c8cf128eb846441.jpg");
	        //ttt = false;
	        // modalData.$links is the list of (filtered) element nodes as jQuery object
	        // modalData.img is the img (or canvas) element for the loaded image
	        // modalData.options.index is the index of the current link
	    });
	    //default 默认自动获取更新,自动播放
	    $("#toggle-fullscreen").click();
	    $("#start-slideshow").click();
	    $("#autobtn").click();
	    $("#vfn").click();
	    $("#test").click(function(){
	    	var modalData = $("#modal-gallery").data('modal');
	    	modalData.startSlideShow();
	    	
/*	    	$('#gallery a:first').click();*/	    	
	    	//$("#soso").click();
	    	//[1]关闭当前页面
	    	//[2]模拟点击第一张图片
	    	//[3]幻灯片播放
	    });
}